<script setup lang="ts">
import { onLaunch, onShow, onHide } from '@dcloudio/uni-app'
import md5 from 'js-md5'
import { fetchSystemInfo, mobileLogin } from './apis/sys/user'
import { useUserStore } from './store/modules/user'
import { apiHost, productBase } from './config'
const userStore = useUserStore()
function searchParse() {
    const resultObj: Record<string, string> = {}
    if (!window) {
      return resultObj
    }
    let search = window.location.search
    if (search && search.length > 1) {
        search = search.substring(1)
        const items = search.split('&')
        for (let index = 0; index < items.length; index++) {
            if (!items[index]) {
                continue
            }
            const kv = items[index].split('=')
            resultObj[kv[0]] = typeof kv[1] === 'undefined' ? '' : kv[1]
        }
    }
    return resultObj
}

const autoLogin = () => {
    // 默认登录操作，如果存在 userid 和 phonenumber 参数，则直接登录操作
    const params = searchParse()
    if (params.phonenumber && params.orgid) {
        const username = `sfa${params.phonenumber}${params.orgid}`;
        const password = md5(username)
        mobileLogin({
            username,
            password
        }).then(resp => {
            // 保存token和uid
            const { uuid, token } = resp
            userStore.setToken(token, uuid)
        })
    }
}
const fetchProductInfo = () => {
    let host
    // #ifdef H5
    host = window.location.hostname
    if (host === 'localhost') {
        host = apiHost.split('://')[1]
    } else {
        host = host + (window.location.port === '' ? '' : ':' + window.location.port)
    }
    host = host + import.meta.env.VITE_PUBLIC_PATH + '/'
    // #endif
    // #ifdef MP-WEIXIN || MP-QQ
    host = productBase
    // #endif
    fetchSystemInfo(host)
        .then(resp => resp.data)
        .then(resp => {
            const { id, tenantId } = resp
            userStore.tenantId = tenantId
            userStore.productId = id
            userStore.appid = import.meta.env.VITE_APP_ID || 'app_preview'
        })
        .catch(e => {
            console.error(e)
        })
}

// onLaunch(() => {
//     console.log('App Launch')
//     fetchProductInfo()
//     autoLogin()
// })
// 监听iframe的消息
const handleMessage = (event: MessageEvent) => {
// #ifdef H5
    // 隐藏页面header
    if (event.data.type === 'hideHeader') {
        const header = document.querySelector<HTMLElement>('uni-page-head');
        if (header) {
          header.style.display = 'none';
        }
        const tabbar = document.querySelector<HTMLElement>('uni-tabbar');
        if (tabbar) {
          tabbar.style.display = 'none';
        }
    }
// #endif
};
// #ifdef H5
window.addEventListener('message', handleMessage, false);
// #endif

</script>
<style lang="scss">
@import './uni_modules/vk-uview-ui/index.scss';
page {
  height: 100%;
  overflow: auto;
  .page_root {
    min-height: 100%;
    box-sizing: border-box;
    overflow: auto;
  }
}
</style>
<style lang="less">
@import './style.less';
</style>
